package com.example.demo.dao.Impl;

import com.example.demo.dao.IAccountDAO;
import com.example.demo.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 根据操作数据库表的接口规范，对其进行实现
 * 编写者：蓝东顺
 */
@Repository
public class AccountDaoImpl implements IAccountDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(Account account) {
        return jdbcTemplate.update("insert into account(name,money) values(?,?)",
                account.getName(),account.getMoney());
    }

    @Override
    public List<Account> getName(String name) {
        return jdbcTemplate.query("select id,name,money from account where name=?",new Object[]{name},
                new BeanPropertyRowMapper<>(Account.class));
    }

    @Override
    public List<Account> getAll() {
        return jdbcTemplate.query("select id,name,money from account ",new Object[]{},
                new BeanPropertyRowMapper<>(Account.class));
    }
}
